package example;

import com.googlecode.tabletags.tagsupport.PagerDisplay;
import com.googlecode.tabletags.tagsupport.Sortable;
import com.opensymphony.xwork2.ActionSupport;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

/**
 * A test action that tests the rendering of the table tag.
 */
public class TableAction extends ActionSupport implements SessionAware,
    Sortable {
    // key in the session where the current page number is stored
    public static final String PAGE_NUMBER = "PAGE_NUMBER";

    public static final String COLUMN_KEY = "COLUMN_KEY";

    public static final String SORT_ORDER = "SORT_ORDER";

    private List items;

    private Map session;

    private String pagerCommand;

    private PagerDisplay pagerDisplay;

    private String sortColumnKey;

    /**
     * @see com.opensymphony.xwork2.ActionSupport#execute()
     */
    public String execute() {
        int currentPage = getPageNumber();
        if(pagerCommand != null) {
            if("next".equals(pagerCommand) && currentPage < 2) {
                currentPage += 1;
            } else if("previous".equals(pagerCommand) && currentPage > 1) {
                currentPage -= 1;
            }
            session.put(PAGE_NUMBER, new Integer(currentPage));
        }
        items = new ArrayList();
        if(currentPage == 1) {
            items.add(new TestBean("red", "1"));
            items.add(new TestBean("orange", "2"));
        } else {
            items.add(new TestBean("blue", "3"));
            items.add(new TestBean("purple", "4"));
        }

        if(sortColumnKey != null) {
            if(sortColumnKey.equals(getSortColumnKey())) {
                if(DESCENDING.equals(getSortOrder())) {
                    session.put(SORT_ORDER, ASCENDING);
                } else {
                    session.put(SORT_ORDER, DESCENDING);
                }
            } else {
                session.put(COLUMN_KEY, sortColumnKey);
                session.put(SORT_ORDER, DESCENDING);
            }
        }
        return SUCCESS;
    }

    protected int getPageNumber() {
        Integer pageNum = (Integer) session.get(PAGE_NUMBER);
        if(pageNum == null) {
            pageNum = new Integer(1);
            session.put(PAGE_NUMBER, pageNum);
        }
        return pageNum.intValue();
    }

    public List getItems() {
        return items;
    }

    public void setItems(List items) {
        this.items = items;
    }

    public void setSession(Map session) {
        this.session = session;
    }

    public String getPagerCommand() {
        return pagerCommand;
    }

    public PagerDisplay getPagerDisplay() {
        return new PagerDisplay(2, getPageNumber(), 4);
    }

    public void setPagerCommand(String pagerCommand) {
        this.pagerCommand = pagerCommand;
    }

    public void setPagerDisplay(PagerDisplay pagerDisplay) {
        this.pagerDisplay = pagerDisplay;
    }

    public String getSortColumnKey() {
        String sortColumnKey = (String) session.get(COLUMN_KEY);
        if(sortColumnKey == null) {
            return "col1";
        }
        return sortColumnKey;
    }

    public String getSortOrder() {
        String sortOrder = (String) session.get(SORT_ORDER);
        if(sortOrder == null) {
            return DESCENDING;
        }
        return sortOrder;
    }

    public void setSortColumnKey(String sortColumnKey) {
        this.sortColumnKey = sortColumnKey;
    }
}
